//
//  VC2ForCell.m
//  PCAForWar
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VC2ForCell.h"
#import "DataBaseHandler.h"
#import "ModelForVC2.h"

@interface VC2ForCell () <UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *webForVC2;
@property (nonatomic, retain) UIActivityIndicatorView *IndicatorView;
@property (nonatomic, retain) NSMutableArray *arrForLike;
@property (nonatomic, assign) BOOL isLike;

@end

@implementation VC2ForCell

- (void)dealloc{
    
    _webForVC2.delegate = nil;
    
    [_webForVC2 release];
    [_IndicatorView release];
    [_arrForLike release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.arrForLike = [NSMutableArray array];
    self.arrForLike = [[DataBaseHandler shareDataBaseHandler] selectAll].mutableCopy;
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Shoucang1"] style:UIBarButtonItemStylePlain target:self action:@selector(shouCang)];
    
    self.navigationItem.rightBarButtonItems = @[item1];
    
    [item1 release];
    
    CGRect frame = self.view.frame;
    
    self.webForVC2 = [[UIWebView alloc ]initWithFrame:CGRectMake(0, -140, CGRectGetWidth(frame), CGRectGetHeight(frame) + 140)];
    
    self.webForVC2.userInteractionEnabled = YES;
    self.webForVC2.delegate = self;
    self.webForVC2.opaque = NO;
    
    [self.view addSubview:self.webForVC2];
    
    NSURL *url = [NSURL URLWithString:self.Location];
    [self.webForVC2 loadRequest:[NSURLRequest requestWithURL:url]];
    
    [self changeColor];
    
    [_webForVC2 release];
    
    [self addIndicatorView];
}


- (void) addIndicatorView {
    
    self.IndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [self.IndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    self.IndicatorView.center = self.view.center;
    
    self.IndicatorView.color = [UIColor blackColor];
    
    [self.view addSubview:self.IndicatorView];
    
    [_IndicatorView release];
    
    [self.IndicatorView startAnimating];
}

#pragma mark 网络视图加载完成的时候调用此方法
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [self.IndicatorView stopAnimating];
}

#pragma mark 收藏点击方法
- (void)shouCang {
    
    if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor cyanColor]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消收藏?" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [[DataBaseHandler shareDataBaseHandler] deleteModelWithTitle:self.Title];
            
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
            
        }];
        
        UIAlertAction *actionCal = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:actionOK];
        [alert addAction:actionCal];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    } else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            // 添加数据
            ModelForVC2 *model = [[ModelForVC2 alloc] init];
            
            model.Title = self.Title;
            model.NewsSummary = self.NewsSummary;
            model.NewsUrl = self.Location;
            model.NewsCommentNum = self.NewsCommentNum;
            
            [[DataBaseHandler shareDataBaseHandler] insertModelYaoWen:model];
            
            [model release];
            
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor cyanColor];
            
        }];
        
        [alert addAction:actionOK];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
}

#pragma mark 判断button颜色 是否收藏过
- (void)changeColor {
    
    self.arrForLike = [NSMutableArray array];
    
    self.arrForLike = [[DataBaseHandler shareDataBaseHandler]selectAll].mutableCopy;
    
    for (ModelForVC2 *model in self.arrForLike) {
        
        if ([model.Title isEqualToString:self.Title]) {
            
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor cyanColor];
            
            self.isLike = YES;
            
            break;
            
        } else {
            
            self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
            
            self.isLike = NO;
            
        }
    }
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
